<?php

class Opinion extends ObjectModel {

    public $id_opinion;
    public $name;
    public $rating;
    public $message;
    public $created_at;
    public $visible;
    protected $table = 'opinion';
    protected $identifier = 'id_opinion';

    public static function getRatingIcon($rating) {
        return str_repeat('<img src="../img/admin/rating.png" alt="" />', (int) $rating);
    }

    public function setVisible($val) {
        if (!Db::getInstance()->Execute('UPDATE `' . _DB_PREFIX_ . $this->table . '` SET `visible`=' . (int) $val . ' WHERE `id_opinion`=' . (int) $this->id_opinion)) {
            $this->_errors[] = Tools::displayError('Wystąpił problem z zapisem');
        }
    }

    public static function fetchAllVisible() {
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
		SELECT `id_opinion`, `name`, `rating`, `message`
		FROM `' . _DB_PREFIX_ . 'opinion` WHERE `visible`=1
		ORDER BY `created_at` DESC LIMIT 5');
    }

}